package com.datatools.ui.basewindow;

import com.datatools.i18n.BaseTextResource;
import com.datatools.i18n.zhCN;
import com.datatools.ui.frame.LeftPanel;
import com.datatools.ui.frame.RightPanel;

import javax.swing.*;
import java.awt.*;
import java.io.Serializable;

/**
 * 主窗体
 */
public class BaseWindow extends JFrame implements Serializable {

    private static final long serialVersionUID=1L;

    private BaseTextResource baseTextResource;

    private BaseWindowAdapter baseWindowAdapter ;
    //左面板
    private LeftPanel leftPanel;
    //右面版
    private RightPanel rightPanel;

    //单例
    private static volatile BaseWindow baseWindow;
    private BaseWindow() {
    }
    public static BaseWindow getInstance() {
            if (null == baseWindow) {
                synchronized (BaseWindow.class) {
                    if (null == baseWindow) {
                        baseWindow = new BaseWindow();
                        baseWindow.setLeftPanel(LeftPanel.getInstance());
                        baseWindow.setRightPanel( new RightPanel());
                        baseWindow.setBaseWindowAdapter( new BaseWindowAdapter());
                        baseWindow.setBaseTextResource(BaseTextResource.getInstance());
                    }
                }
            }
        return baseWindow;
    }

    //防止反序列化破坏单例
    public Object readResolve() {
        return getInstance();
    }

    public void buildGUI(){
        //构造LeftPanel
        this.leftPanel.buildGUI();

        this.setTitle(baseTextResource.getBaseWindowTitle());
        this.setResizable(true);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //获取屏幕尺寸
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(screensize);
        //设置布局
        this.setLayout(new BorderLayout());

        //菜单栏
        JMenuBar menuBar = new JMenuBar();
        //JMenuBar的Border未实现序列文化接口
        menuBar.setBorder(null);
        this.add(menuBar,BorderLayout.NORTH);

        //文件菜单
        JMenu fileMenu = new JMenu(baseTextResource.getFileMenu());
        JMenuItem create = new JMenuItem(baseTextResource.getCreateFile());
        fileMenu.add(create);
        menuBar.add(fileMenu);

        //添加左右面板
        //左面板的尺寸
        Dimension leftPanelDimension = new Dimension();
        leftPanelDimension.setSize(300,screensize.height);
        leftPanel.setMinimumSize(leftPanelDimension);
        //分割面板
        JSplitPane splitPane = new JSplitPane();
        //让分割线显示出箭头
        splitPane.setOneTouchExpandable(true);
        //操作箭头，重绘图形
        splitPane.setContinuousLayout(true);
        //设置分割线方向，左右分割
        splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setLeftComponent(leftPanel);
        splitPane.setRightComponent(rightPanel);
        //设置分割线的宽度
        splitPane.setDividerSize(5);
        //设定分割线的距离左边的位置
        splitPane.setDividerLocation(300);
        this.add(splitPane,BorderLayout.CENTER);

        //添加事件处理
        this.addWindowListener(baseWindowAdapter);
    }




    public BaseTextResource getBaseTextResource() {
        return baseTextResource;
    }

    public void setBaseTextResource(BaseTextResource baseTextResource) {
        this.baseTextResource = baseTextResource;
    }

    public LeftPanel getLeftPanel() {
        return leftPanel;
    }

    public void setLeftPanel(LeftPanel leftPanel) {
        this.leftPanel = leftPanel;
    }

    public RightPanel getRightPanel() {
        return rightPanel;
    }

    public void setRightPanel(RightPanel rightPanel) {
        this.rightPanel = rightPanel;
    }

    public BaseWindowAdapter getBaseWindowAdapter() {
        return baseWindowAdapter;
    }

    public void setBaseWindowAdapter(BaseWindowAdapter baseWindowAdapter) {
        this.baseWindowAdapter = baseWindowAdapter;
    }
}
